<?php
/**
 * 库存service
 * 有关库存方面的扩展功能都可以使用此服务
 * @author dongqiujing
 * @package ome_service_stock
 * @copyright www.shopex.net.cn 2010.10.14
 *
 */
class ome_service_stock{
    
    /**
     * 清除预占库存
     * @access public
     * @param int $order_id 订单ID
     */
    public function clean_freeze($order_id){
        kernel::single("ome_rpc_request_stock")->clean_freeze($order_id);
    }
    
    /**
     * 更新库存
     * @access public
     * @param array $stocks 需更新的货号数量多维数组 array('bn'=>'22')
     * @param int $shop_id 店铺ID
     * @param string $shop_type 店铺类型
     */
    public function update_stock($stocks,$shop_id,$shop_type=''){
        kernel::single("ome_rpc_request_stock")->stock_update($stocks,$shop_id,$shop_type);
    }
	
	/**
	 * 计算回写的最大库存值
	 * @access public
	 * @param string $shop_id 店铺ID
	 * @param int $store_sync_from 回写开始时间
	 * @param int $store_sync_end 回写结束时间
	 * @param int $limit 一次性回写的库存个数
	 * @param int $offset 当前回写的库存位置
	 * @return array 
	 */
	public function calculate_stock($shop_id,$store_sync_from='',$store_sync_end='',$offset='0',$limit='100'){

	    //TODO：可在店铺上进行设置是否回写库存
        $shop_stock_config = &app::get('ome')->getConf('shop_stock_config_'.$shop_id);
       // $shop_stock_config = 'true';//TODO:默认为true
        kernel::log("shop_stock_config:".$shop_id.$shop_stock_config);
        if ($shop_stock_config == 'true'){
            $where = " `max_store_lastmodify`>'".$store_sync_from."' AND `max_store_lastmodify`<='".$store_sync_end."' AND `max_store_lastmodify` IS NOT NULL ";
            $sql = "SELECT `product_id`,`bn`,`store_freeze` 
                    FROM `sdb_ome_products` 
                    WHERE $where LIMIT ".$offset.",".$limit;
            $bn_list = kernel::database()->select($sql);
          
            
                    
            $stocks = array();
            if($bn_list){
            	$pkgbn=array();
//             	$sql_pkg="select pkg_bn from sdb_omepkg_pkg_goods";
//             	$pkgbn_list = kernel::database()->select($sql_pkg);
//             	foreach($pkgbn_list as $k=>$v){
//             		$pkgbn[]=$v['pkg_bn'];            	
//             	}
            	
            	$products="";
                foreach($bn_list as $k=>$v){
                    //获取bn线上仓库库存总和
                    $sql = " SELECT sum(bp.store) as store FROM `sdb_ome_branch_product` bp,`sdb_ome_branch` b 
                             WHERE bp.`product_id`='".$v['product_id']."' and bp.`branch_id`=b.`branch_id` AND b.`attr`='true' ";
                    $total_store = kernel::database()->selectrow($sql);
                    if (IS_NULL($total_store['store']) || $total_store['store'] === '') continue;
                    
                    //取得sku_id
                    $sql_sku="select addon,sku,node_id from sdb_ome_product_sku_map where bn='".$v['bn']."'";
                    $skus = kernel::database()->select($sql_sku);
                    $skuArr=array();
                    foreach ($skus as $sk=>$sv){
                    	$skuArr[$sv['node_id']]=array('sku'=>$sv['sku'],'addon'=>$sv['addon']);                    	
                    }
                    
                    //回写库存值 = 线上仓库库存总和 - 预占库存
                    $quantity = $total_store['store'] - $v['store_freeze'];
                    if ($quantity < 0) $quantity = 0;
                    
                    //排除捆绑
                    //if(!in_array($v['bn'], $pkgbn)){

                    	$stocks[] = array(
                    			'bn' => trim($v['bn']),
                    			'quantity' => $quantity,
                    			'skus' => $skuArr,
                    	);

                    	$products=$products.",".$v['product_id'];
                    //}
                    
                }
                if($products){
                	$products=substr($products, 1);
                	//调用服务接口查询捆绑商品库存
                	if ($service_order = kernel::servicelist('service.order')){
                		foreach($service_order as $object=>$instance){
                			if(method_exists($instance, 'getPKGStore')){
                				$pkgStore = $instance->getPKGStore($products);
                				$stocks = array_merge($stocks, $pkgStore);
                			}
                		}
                	}
          
                	
                }
                
                return $stocks;
            }
        }else{
            return null;
        }
	}
	
}

